package com.xixi.server;

import com.xixi.model01.Student;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;

//服务器的初始化和关闭
public class ServerInitClose {
    //自定义成员方法实现服务器的初始化操作
   // Socket socket=null;
    //成员变量进行调用
    private ServerSocket serverSocket;
    private Socket socket;
    private ObjectInputStream objectInputStream;
    private ObjectOutputStream objectOutputStream;

    public ObjectInputStream getObjectInputStream() {
        return objectInputStream;
    }
    public ObjectOutputStream getObjectOutputStream() {
        return objectOutputStream;
    }

    //往这里准备写学员信息
    private List<Student> studentList=null;
    private File stuFile=new File("D:\\code\\students.massage");
    private ObjectOutputStream stuOos=null;
    private ObjectInputStream stuOis=null;
    public List<Student> getStudentList(){
        return studentList;
    }
    public void serverInit() throws IOException {
      /*  1.创建ServerSocket类型的对象并提供端口号
      * 2.等待客户端的连接请求，调用accept方法
      * 3.使用输入输出流进行通信
      * 4。关闭Scoket并释放有关资源
      * */
        /*ServerSocket*/ serverSocket=new ServerSocket(10086);
        //为了使代码更清晰，不复杂，所以向外抛出
        System.out.println("等待客户端的连接请求。。");
        /*Socket*/ socket=serverSocket.accept();
        System.out.println("客户端连接成功，，，");
       // ObjectOutputStream objectOutputStream=new ObjectOutputStream(new FileOutputStream());
       // ObjectInputStream objectInputStream=new ObjectInputStream(new FileInputStream());
        /*ObjectInputStream*/ objectInputStream=new ObjectInputStream(socket.getInputStream());
        /*ObjectOutputStream*/ objectOutputStream=new ObjectOutputStream(socket.getOutputStream());
        System.out.println("服务器初始化成功");
        initStudentList();
    }

    private void initStudentList() {
       // List<Student> stuList = null;
        if (stuFile.exists()) {
            try {
                ObjectInputStream stuOis = new ObjectInputStream(new FileInputStream(stuFile));
                studentList=(List<Student>)stuOis.readObject();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }else {
            studentList = new ArrayList<>();
        }
        System.out.println("初始化后，学员信息为："+studentList);
    }
    /**
     * 保存学员信息到文件中D:\code\test5\students.shitou
     */
    public void saveStuList(){
        try {
            /*ObjectOutputStream*/ stuOos = new ObjectOutputStream(new FileOutputStream(stuFile));
            //Object stuList = null;
            stuOos.writeObject(studentList);
            stuOos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //自定义成员方法实现服务器的关闭操作
    //都进行抛出异常
    public void serverClose() throws IOException {
        //关闭Scoket并释放有关资源
        objectOutputStream.close();
        objectInputStream.close();
        socket.close();
        serverSocket.close();
        System.out.println("服务器关闭成功");
    }
}
